package com.xpec.c4.service.module.datacenter.account;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.service.module.account.Account;
import com.ms.service.module.account.db.AccountDAO;
import com.xpec.c4.game.datamanager.BaseDataInfo;
import com.xpec.c4.game.datamanager.ResultNo;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.service.module.datacenter.DCAsyncCommand;


/**
 * 
 * @author yangbingyan
 *
 */
public class SaveAccountDataAsync extends DCAsyncCommand{

	/**
	 * 
	 */
	protected AccountDataManager dataManager;
	
	/**
	 * 
	 */
	private static final Log log = LogFactory.getLog(SaveAccountDataAsync.class);
	
	/**
	 * 
	 */
	protected int handle;

	
	public int getHandle() {
		return handle;
	}

	public void setHandle(int handle) {
		this.handle = handle;
	}
	
	@Override
	public void asyncExecute() {
		
		BaseDataInfo info = this.getDataInfo();
		
		if(info.getData() != null){
			Account acc = (Account)info.getData();
			GameUtility.writeCashUpdateConsoleLog("SaveAccountDataAsync.asyncExecute()", acc.getAccountId().toString(), 0, 0, acc.getCash());
			boolean flag = AccountDAO.saveOrUpdateAccount(acc);
		
			if(!flag){
				log.error("cannot update Account:" + this.getDataInfo().getKey() );
				resultCode = ResultNo.DB_OPERATION_FAILED;
			}else{
				resultCode = ResultNo.OK;
				info.setHandle(handle);
			}
		}else{
			resultCode = ResultNo.DATA_NOT_FOUND;
		}
		
	}

	@Override
	public void execute() {
		// callback
		if(dataManager != null){
			dataManager.writeDataCallback(resultCode, dataInfo, srcModuleID, null);
		}
	}
	
	public void setDataManager(AccountDataManager dataManager) {
		this.dataManager = dataManager;
	}


}
